﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using DaInterFace;

namespace DataApplication
{
    public class Access
    {

        private static OleDbConnection _connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D.MDB;Jet OLEDB:Database Password=3201985;");
        public static DataBaseMessage AccessMessage;

        public static DataSet GetData(String sql)
        {
            DataSet data = new DataSet();
            try
            {
                lock (_connection)
                {
                    OleDbDataAdapter adapter = new OleDbDataAdapter(sql, _connection);
                    adapter.Fill(data);
                }
            }
            catch (Exception ex)
            {
                if (AccessMessage != null)
                {
                    AccessMessage.Invoke(MessageType.Error, ex.Message, 0);
                }
            }

            return data;
        }

        public static int Update(String sql)
        {
            int count = 0;
            try
            {
                lock (_connection)
                {
                    if (_connection.State != ConnectionState.Open)
                    {
                        _connection.Open();
                    }
                    OleDbCommand cmd = new OleDbCommand(sql, _connection);
                    count = cmd.ExecuteNonQuery();
                    _connection.Close();
                }
            }
            catch (Exception ex)
            {
                if (AccessMessage != null)
                {
                    AccessMessage.Invoke(MessageType.Error, ex.Message, 0);
                }
            }

            return count;
        }
    }
}
